﻿using System;
using System.Collections.Generic;
using eCommerce.db.DALRespository;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;
using eCommerce.core.Helpers;
using System.Web;
using System.Web.Caching;

namespace eCommerce.db.BORespository
{
    public class ArticleBo
    {
        public static List<ArticleEntity> GetListPagination(int categoryId, int pageIndex, int pageSize, ref int totalRecords)
        {
            List<ArticleEntity> list = null;
            var cacheName = Utils.GET_ARTICLE_LISTPAGINATION(categoryId, pageIndex, pageSize);

            list = Utils.GetFromCacheIIS<List<ArticleEntity>>(cacheName);
            if (list == null)
            {
                list = ArticleDal.GetListPagination(categoryId, pageIndex, pageSize, ref totalRecords);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;

        }

        public static List<ArticleEntity> Search(string keyword, int categoryId, int status, int orderBy, int pageIndex, int pageSize, ref int totalRecords)
        {
            return ArticleDal.Search(keyword, categoryId, status, orderBy, pageIndex, pageSize, ref totalRecords);
        }

        public static List<ArticleEntity> GetFocusArticle(int count)
        {
            return ArticleDal.GetFocusArticle(count);
        }

        public static List<ArticleEntity> GetMostView(int top)
        {
            return ArticleDal.GetMostView(top);
        }      

        public static ArticleEntity GetById(int id)
        {
            return ArticleDal.GetById(id);
        }

        public static ArticleEntity GetByCatId(int categoryId)
        {
            ArticleEntity list = null;
            var cacheName = Utils.GET_ARTICLE_GETBYCATID(categoryId);

            list = Utils.GetFromCacheIIS<ArticleEntity>(cacheName);
            if (list == null)
            {
                list = ArticleDal.GetByCatId(categoryId);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;
        }

        public static void Save(ArticleEntity o)
        {
            ArticleDal.Save(o);
        }

        public static void ChangeStatus(int approvedBy, int articleId, int status)
        {
            ArticleDal.ChangeStatus(approvedBy, articleId, status);
        }

        public static void Delete(int id)
        {
            ArticleDal.Delete(id);
        }
    }
}
